/* 
  封装发送请求的基类(父类)     派生类(子类)
  意义： 保证有发送请求的能力
*/

import type { RequestInterface } from '@/compents/interfaces';
const BASEURL = 'https://gmall-prod.atguigu.cn/skb'
class Service {
  /* 发送请求的核心功能函数 */
  api(params: RequestInterface): any{
    return new Promise((resolve, reject) => {
      // 发送请求
      uni.request({
        url: BASEURL + params.url,
        data: params.data,
        method: params.method,
        success: (res) => {
          console.log(res);
          resolve(res.data)
        },
        fail: (err) => {
          console.log(err);
          reject(err);
        }
      })
    })
  }

  /* 简化调用的参数传递，并且语义化更加明确 */
  get(options: RequestInterface){
    options.method = 'GET'
    return this.api(options)
  }
  post(options: RequestInterface){
    options.method = 'POST'
    return this.api(options)
  }
  put(options: RequestInterface){
    options.method = 'PUT'
    return this.api(options)
  }
  delete(options: RequestInterface){
    options.method = 'DELETE'
    return this.api(options)
  }
}

// let s = new Service()
// s.api({
//   url: '',
//   data: {},
//   method: 'POST'
// })
// s.get({
//   url: '',
//   data: {}
// });
// s.post();
export default Service;

